<?php
/**
 * Created by PhpStorm.
 * User: yangwang
 * Date: 2024-04-26
 * Time: 19:37
 */

namespace App\Service\Cruise\Shop;


use App\Models\Cruise\CruiseShopLicenseRecordsLogModel;
use App\Models\Cruise\CruiseShopLicenseResourceRecordsModel;
use App\Remote\Rms\RmsUserRemote;
use App\Service\BaseService;

class CruiseShopLicenseRecordsLogService extends BaseService
{
    public static function getRecordLog(int $shopId, int $pageSize, int $pageNum): array
    {
        $paginate = CruiseShopLicenseRecordsLogModel::query()
                                                    ->where('shopId', $shopId)
                                                    ->orderBy('id', 'desc')
                                                    ->paginate($pageSize, ['*'], $pageName = 'page', $pageNum);

        $userIds = collect($paginate->items())->pluck('createdByUid')->unique()->values()->toArray();

        $users = $userIds
            ? array_column(
                RmsUserRemote::getRmsUserByIds($userIds, ['id', 'username']),null,'id'
            )
            : [];

        return BaseService::pageLists($paginate, function ($item) use ($users) {

            $createdUsername = $users[$item->createdByUid]['username'] ?? '未记录';

            return [
                'createdAt'   => $item->createdAt->format('Y-m-d H:i:s'),
                'operateDesc' => CruiseShopLicenseRecordsLogModel::OPERATE_TYPE_MAP[$item->operateType] . ' : ' . ($item->createdByUid == 0 ? ' - ' : $createdUsername),
                'actionDesc'  => CruiseShopLicenseRecordsLogModel::ACTION_TYPE_MAP[$item->actionType] . ' : ' . CruiseShopLicenseResourceRecordsModel::TYPE_MAP[$item->type] ?? '',
                'remark'      => $item->remark ?: '-'
            ];
        });
    }
}